package adapter;/*
 *@创建者 lnn
 *@创建时间2016/8/300:21
 *@描述 ${TODO}
*/

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.boltshow.R;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import model.BoltCatIdModel;

public class CatIdBaseAdapter extends BaseAdapter {
    private Context mContext;
    private List<BoltCatIdModel.DataBean> mData;
    private ImageOptions mImageOptions;

    public CatIdBaseAdapter(List<BoltCatIdModel.DataBean> data, Context context) {
        mData = data;
        mContext = context;
        mImageOptions = new ImageOptions.Builder().setUseMemCache(true).build();

    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_catid, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        BoltCatIdModel.DataBean dataBean = mData.get(position);
        x.image().bind(holder.ivShow, dataBean.getImgUrl(), mImageOptions);
        holder.tvTitle.setText(dataBean.getTitle());
        holder.tvTime.setText(dataBean.getSaleTime());
        String flashDown = dataBean.getFlashDown();
        SpannableStringBuilder builder = new SpannableStringBuilder(flashDown);
        builder.setSpan(new ForegroundColorSpan(Color.RED), 0,flashDown.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.tvFlashDown.setText(flashDown);
        return convertView;
    }

    public static class ViewHolder {
        private ImageView ivShow;
        private TextView tvTitle, tvFlashDown, tvTime;

        public ViewHolder(View view) {
            ivShow = (ImageView) view.findViewById(R.id.iv_item_catid_show);
            tvFlashDown = (TextView) view.findViewById(R.id.tv_item_catid_flashDown);
            tvTime = (TextView) view.findViewById(R.id.tv_item_catid_saleTime);
            tvTitle = (TextView) view.findViewById(R.id.tv_item_catid_title);
        }
    }
}
